from django.contrib.auth.models import User
from django import forms
class RegisterForm(forms.Form):
    username = forms.CharField(max_length=30)
    email = forms.EmailField()
    password1 = forms.CharField(max_length=30,widget=forms.PasswordInput(render_value=False))
    def clean_username(self):
        try:
             User.objects.get(username=self.cleaned_data['username'])
        except User.DoesNotExist:
             return self.cleaned_data['username']
        raise forms.ValidationError("This username is already in use.Please choose another.")

    def clean_email(self):
        try:
            User.objects.get(email=self.cleaned_data['email'])
        except User.DoesNotExist:
            return self.cleaned_data['email']
        raise forms.ValidationError("This email is already in use.Please choose another.")
    def save(self):
        new_user = User.objects.create_user(username=self.cleaned_data['username'],
                                             email=self.cleaned_data['email'],
                                             password=self.cleaned_data['password1'])
        return new_user

class LoginForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(max_length=30,widget=forms.PasswordInput(render_value=False))
    def clean_email(self):
        try:
            User.objects.get(email=self.cleaned_data['email'])
            return self.cleaned_data['email']
        except User.DoesNotExist:
            forms.ValidationError("This email is not registertion,Please registion.")
